<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    left: {
                        val: 6,
                    },
                    right: {
                        val: 7,
                    },
                },
            }
            function bfs(root) {
                let result = []
                let queue = [root]
                while (queue.length) {
                    let len = queue.length
                    result.push([])
                    for (let i = 0; i < len; i++) {
                        let node = queue.shift()
                        result[result.length - 1].push(node.val)
                        if (node.left) {
                            queue.push(node.left)
                        }
                        if (node.right) {
                            queue.push(node.right)
                        }
                    }
                }
                console.log(result)
            }
            bfs(tree)
        </script>
    </body>
</html>
